from django.db import models
from db_cc.base_model import BaseModel
from tinymce.models import HTMLField
# Create your models here.

class GoodsCategory(BaseModel):
    '''商品的总分类表首页下拉菜单'''
    CATEGORY_TYPE = (
        (1, "一级类目"),
        (2, "二级类目"),
        (3, "三级类目"),
    )
    name = models.CharField(verbose_name='类别名', default="", max_length=30, help_text="类别名")
    code = models.CharField(verbose_name="类别code", default="", max_length=50, help_text="类别code")
    desc = models.TextField(verbose_name="类别描述", default="", help_text="类别描述")
    parents_id = models.IntegerField(verbose_name='父分类id',default="",help_text='父分类')
    # 目录树级别
    category_type = models.IntegerField(verbose_name="类目级别", choices=CATEGORY_TYPE, help_text="类目级别")
    # 设置models有一个指向自己的外键
    parent_category = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True, verbose_name="父类目级别",
                                        help_text="父目录",
                                        related_name="sub_cat")
    is_tab = models.BooleanField(verbose_name="是否导航", default=False, help_text="是否导航")
    # add_time = models.DateTimeField(verbose_name="添加时间", default=datetime.now)
    is_logo = models.ImageField(verbose_name='logo_image',upload_to='type',null=True)
    class Meta:
        db_table = 'goods_category'
        verbose_name = "商品类别"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class GoodsType(BaseModel):
    """商品类型模型列表页的type"""
    name = models.CharField(max_length=20,verbose_name='种类名称')
    logo = models.CharField(max_length=20,verbose_name='标识')
    image = models.ImageField(upload_to='type', verbose_name='商品类型图片')

    class Meta:
        db_table = 'df_type'
        verbose_name = '商品种类'
        verbose_name_plural =  verbose_name

    def __str__(self):
        return self.name
#商品品牌表
class GoodsBrandInfo(BaseModel):
    brand_name  = models.CharField(max_length=100,verbose_name='品牌名称')
    brand_logo  = models.ImageField(upload_to='banner',verbose_name='品牌logo图')
    brand_desc  = models.CharField(max_length=200,verbose_name='品牌描述')
    brand_status = models.BooleanField(default=1,verbose_name='品牌状态')
    brand_order = models.IntegerField(verbose_name='品牌排序')

    class Meta:
        db_table = 'goods_brand_info'
        verbose_name = '品牌信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.brand_name
#规格表
class GoodsSpecifications(BaseModel):
    guige_name = models.CharField(max_length=200,verbose_name='规格名称')
    guige_code = models.CharField(max_length=20,verbose_name='规格编号')
    spu_id     = models.ManyToManyField('GoodsSpu', verbose_name='多对多SPUid',through='GoodsSpecificationsAndSpu')
    class Meta:
        db_table = 'goods_specifications'
        verbose_name = '规格表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.guige_name
#规格值
class GoodsSpecificationsValue(BaseModel):
    guige_id     = models.ForeignKey('GoodsSpecifications',verbose_name='规格表id',on_delete=models.CASCADE)
    guige_value  = models.CharField(max_length=200,verbose_name='规格值')
    guige_danwei = models.CharField(max_length=30,verbose_name='规格单位',null=True)
    sku_id = models.ManyToManyField('GoodsSku',verbose_name='多对多Skuid',through='GoodsSpecificationsSkuAndValue')
    class Meta:
        db_table = 'goods_specifications_value'
        verbose_name = '规格值'
        verbose_name_plural = verbose_name

#多对多规格对spu
class GoodsSpecificationsAndSpu(BaseModel):
    guige_id = models.ForeignKey('GoodsSpecifications',on_delete=models.CASCADE)
    spu_id = models.ForeignKey('GoodsSpu',on_delete=models.CASCADE)

    class Meta:
        db_table = 'Goods_guige_and_spu'
        verbose_name = '多对多规格对spu'
        verbose_name_plural = verbose_name

#多对多规格值对sku规格值
class GoodsSpecificationsSkuAndValue(BaseModel):
    guige_value_id = models.ForeignKey('GoodsSpecificationsValue',on_delete=models.CASCADE)
    sku_id = models.ForeignKey('GoodsSku',on_delete=models.CASCADE)

    class Meta:
        db_table = 'goods_specifications_sku_and_value'
        verbose_name = '多对多规格值对sku规格值'
        verbose_name_plural = verbose_name



class IndexTypeGoodsBanner(BaseModel):
    '''首页分类商品展示模型类'''
    DISPLAY_TYPE_CHOICES = (
        (0, '标题'),
        (1, '图片')
    )
    type = models.ForeignKey('GoodsType' ,verbose_name='商品类型',on_delete=models.SET_NULL,null=True,blank=True)
    sku = models.ForeignKey('GoodsSku', verbose_name='商品SKU', on_delete=models.SET_NULL,null=True,blank=True)
    display_type = models.SmallIntegerField(default=1, choices=DISPLAY_TYPE_CHOICES,verbose_name='是否显示')
    index = models.SmallIntegerField(default=0,verbose_name='展示顺序')

    class Meta:
        db_table = 'index_type_goods_banner'
        verbose_name = '主页分类展示商品'
        verbose_name_plural = verbose_name

class GoodsSpu(BaseModel):
    """商品SPU模型类"""
    name = models.CharField(max_length=20,verbose_name='商品SUP名称')
    detail = HTMLField(blank=True,verbose_name='商品详情')
    type_id = models.ForeignKey('GoodsCategory',verbose_name='分类id',on_delete=models.CASCADE)

    class Meta:
        db_table = 'goods_spu'
        verbose_name = '商品SPU'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
class GoodsAttribute(BaseModel):
    '''属性表'''
    name = models.CharField(max_length=200,verbose_name='属性名称')
    type_id = models.ForeignKey('GoodsCategory',verbose_name='关联分类',on_delete=models.CASCADE)

    class Meta:
        db_table = 'goods_attribute'
        verbose_name = '商品属性'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name

class GoodsAttributeValue(BaseModel):
    '''属性值'''
    attribute_id = models.ForeignKey('GoodsAttribute',verbose_name='关联属性id',on_delete=models.CASCADE)
    value = models.CharField(verbose_name='值',max_length=30)

    class Meta:
        db_table = 'goods_attribute_value'
        verbose_name = '商品属性值'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.value

class GoodsAttributeSku(BaseModel):
    '''属性值和sku多对多'''
    sku_id = models.ForeignKey('GoodsSku', verbose_name='商品skuid', on_delete=models.CASCADE)
    attributevalue_id = models.ForeignKey('GoodsAttributeValue', verbose_name='关联属性id', on_delete=models.CASCADE)

    class Meta:
        db_table = 'goods_attribute_sku'
        verbose_name = '商品属性'
        verbose_name_plural = verbose_name

class GoodsSku(BaseModel):
    '''sku表'''
    status_choices = (
        (0, '下线'),
        (1, '上线'),
    )
    type = models.ForeignKey('GoodsType', verbose_name='商品种类', on_delete=models.CASCADE)
    goods = models.ForeignKey('GoodsSpu', verbose_name='商品SPU', on_delete=models.CASCADE)
    category = models.ForeignKey('GoodsCategory', on_delete=models.CASCADE, verbose_name="商品类目")
    name  = models.CharField(max_length=20, verbose_name='商品名称')
    desc  = models.CharField(max_length=255, verbose_name='商品简介')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品价格')
    unite = models.CharField(max_length=20, verbose_name='商品单位')
    image = models.ImageField(upload_to='goods', verbose_name='商品图片')
    stock = models.IntegerField(default=1, verbose_name='商品库存')
    sales = models.IntegerField(default=0, verbose_name="商品销量")
    status = models.SmallIntegerField(default=1, choices=status_choices, verbose_name='商品状态')
    goods_sn = models.CharField("商品唯一货号", max_length=50, default="")
    # 首页中新品展示
    is_new = models.BooleanField("是否新品", default=False)
    # 商品详情页的热卖商品，自行设置
    is_hot = models.BooleanField("是否热销", default=False, help_text='是否热销')
    #积分
    integral = models.IntegerField(verbose_name='商品消费积分',default=26)
    #品牌
    brand_id = models.ForeignKey('GoodsBrandInfo',on_delete=models.CASCADE,verbose_name='品牌外键',null=True)

    class Meta:
        db_table = 'goods_sku'
        verbose_name = '商品信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
#图片sku商品表
class GoodsImages(BaseModel):
    sku_id    = models.ForeignKey('GoodsSku',verbose_name='关联商品id',on_delete=models.CASCADE)
    pic_url   = models.ImageField(upload_to='banner',verbose_name='图片位置路径')
    is_master = models.BooleanField(verbose_name='是否为主图',default=0)
    pic_index = models.IntegerField(verbose_name='图片排序')
    pic_status = models.BooleanField(verbose_name='图片状态',default=1)

    class Meta:
        db_table = 'goods_images'
        verbose_name = '商品图片信息'
        verbose_name_plural = verbose_name

#新闻类型
class IndexNewsInformationType(BaseModel):

    name = models.CharField(max_length=200, blank=True, null=True)

    class Meta:
        db_table = 'index_news_information_type'
        verbose_name = '首页新闻类型'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

#新闻信息
class IndexNewsInfomations(BaseModel):

    name = models.CharField(max_length=255, blank=True, null=True)
    type = models.ForeignKey('IndexNewsInformationType', on_delete=models.DO_NOTHING)
    url = models.CharField(max_length=255, blank=True, null=True)
    index = models.SmallIntegerField(default=0, verbose_name='展示顺序')

    class Meta:
        db_table = 'index_news_information'
        verbose_name = '首页新闻资讯'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

#活动促销
class IndexPromotionBanner(BaseModel):
    '''首页促销活动模型'''
    name = models.CharField(max_length=20, verbose_name='活动促销')
    # url = models.URLField(verbose_name= '活动链接')
    url = models.CharField(max_length=255,verbose_name='活动链接')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品价格',default=200)
    image = models.ImageField(upload_to='banner', verbose_name='活动图片')
    index = models.SmallIntegerField(default=0 , verbose_name='展示顺序')

    class Meta:
        db_table = 'index_promotion_banner'
        verbose_name = '主页促销活动'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

#轮播图
class IndexGoodsBanner(BaseModel):
    '''首页轮播商品展示模型类'''

    sku = models.ForeignKey('GoodsSku',verbose_name='商品',on_delete=models.SET_NULL,null=True,blank=True)
    image = models.ImageField(upload_to='banner',verbose_name='图片')
    index = models.SmallIntegerField(default=0,verbose_name='展示顺序')

    class Meta:
        db_table = 'index_goods_banner'
        verbose_name = '首页轮播商品'
        verbose_name_plural = verbose_name
#限时促销活动
class IndexLimitedTimeSaleForm(models.Model):
    '''活动限时销售'''
    name = models.CharField(max_length=255, blank=True, null=True)
    url = models.CharField(max_length=255, blank=True, null=True)
    image = models.ImageField(upload_to='banner',verbose_name='图片',null=True,default='')
    time_count_start = models.DateTimeField(blank=True, null=True)
    time_count_stop = models.DateTimeField(blank=True, null=True)
    price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
    is_big_title = models.BooleanField(default=0)

    class Meta:
        db_table = 'index_limited_time_sale_form'
        verbose_name = '首页促销活动'
        verbose_name_plural = verbose_name

#分类商品的 活动图(2小图)
class IndexGoodsTypeActivityImage(BaseModel):

    type_id = models.ForeignKey('GoodsType',on_delete=models.CASCADE)
    image = models.ImageField(upload_to='banner',verbose_name='图片位置')
    is_status = models.BooleanField(default=1,verbose_name='状态完整')
    index = models.SmallIntegerField(default=0,verbose_name='展示顺序')

    class Meta:
        db_table = 'index_goods_type_activity'
        verbose_name = '分类活动图'
        verbose_name_plural = verbose_name
